package com.goldfish666.remote_control.components;

import com.goldfish666.remote_control.util.SocketHelper;
import com.goldfish666.remote_control.util.TVInputHelper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.List;

@Slf4j
@Component
@AllArgsConstructor(onConstructor = @__({@Autowired}))
public class TVInputDisplayer {

    private final TVInputHelper inputHelper;

    private DatagramSocket buildDatagramSocket() {
        SocketHelper.buildDatagramSocket(8090);
        return SocketHelper.getDatagramSocket();
    }

    private DatagramPacket buildDatagramPacket() {
        byte[] buf = new byte[200];
        return new DatagramPacket(buf, buf.length);
    }

    private String getIndexValue(List<Byte> byteList) {
        StringBuilder sb = new StringBuilder();
        sb.append("输入：");
        for (byte i : byteList) {
            sb.append(i);
            sb.append(" ");
        }
        return sb.toString();
    }

    private String getIndexValue(List<Byte> byteList, List<Integer> indexList) {
        StringBuilder sb = new StringBuilder();
        sb.append("特定值：");
        for (Integer i : indexList) {
            sb.append(byteList.get(i));
            sb.append(" ");
        }
        return sb.toString();
    }

    public void display(List<Integer> indexList) {
        DatagramSocket datagramSocket = buildDatagramSocket();
        int count = 0;
        while (true) {
            DatagramPacket dp = buildDatagramPacket();
            try {
                datagramSocket.receive(dp);//通过服务的receive方法将收到数据存入数据包中,receive()为阻塞式方法

                List<Byte> standardValue = inputHelper.getStandardValue(dp.getData());

                if (inputHelper.inputAccept(standardValue)) {
                    count++;
                    log.info(count + "");
                    log.info(getIndexValue(standardValue));
                    log.info(getIndexValue(standardValue, indexList));
                } else {
                    log.info("输入不符合规范");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void display() {
        DatagramSocket datagramSocket = buildDatagramSocket();
        int count = 0;
        while (true) {
            DatagramPacket dp = buildDatagramPacket();
            try {
                datagramSocket.receive(dp);//通过服务的receive方法将收到数据存入数据包中,receive()为阻塞式方法

                List<Byte> standardValue = inputHelper.getStandardValue(dp.getData());

                if (inputHelper.inputAccept(standardValue)) {
                    count++;
                    log.info(count + "");
                    log.info(getIndexValue(standardValue));
                } else {
                    log.info("输入不符合规范");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
